<template>
  <div>
    <div class="title">
      <div class="label">风险人群筛选条件</div>
      <el-button v-if="checkAuthority('/stu/rules') && currnetParamType === '学生'" type="primary" size="small" @click="handleRules">风险研判规则</el-button>
      <el-button v-if="checkAuthority('/livewith/rules') && currnetParamType === '同住人'" type="primary" size="small" @click="handleRules">风险研判规则</el-button>
      <!-- <div class="time">数据更新时间：{{updatedTime}}</div> -->
    </div>
    <el-card class="header-card">
      <!-- 一期查询 先注释 防止后需又要改回来 -->
      <!-- <el-form
        ref="filterForm"
        :inline="true"
        :model="queryParams"
        label-width="120px"
      >
        <el-form-item label="体温≥" label-width="55px" prop="bodyTemperature">
          <el-input
            size="mini"
            style="width: 70px"
            v-model.trim="queryParams.bodyTemperature"
            oninput ="value=value.replace(/[^0-9.]/g,'')"
          ></el-input>
          ℃
        </el-form-item>
        <el-form-item label="途径中高风险地区" label-width="180px" prop="passRiskAddr">
          <el-radio-group v-model="queryParams.passRiskAddr">
            <el-radio label="是">是</el-radio>
            <el-radio label="否">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="疫苗" prop="vaccineStatus">
          <el-checkbox-group v-model="queryParams.vaccineStatus">
            <el-checkbox label="加强针">加强针</el-checkbox>
            <el-checkbox label="接种1针、接种2针">接种1针/2针</el-checkbox>
            <el-checkbox label="未接种">未接种</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="健康码" prop="healthCodeStatus">
          <el-checkbox-group v-model="queryParams.healthCodeStatus">
            <el-checkbox label="黄码">黄</el-checkbox>
            <el-checkbox label="红码">红</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label=" " prop="antigenStatus">
          无
          <el-input
            oninput ="value=value.replace(/[^0-9.]/g,'')"
            size="mini"
            style="width: 70px"
            v-model.number.trim="queryParams.antigenStatus"
          ></el-input>
          小时内核酸阴性报告
        </el-form-item>
        <el-form-item label=" " prop="temperature">
          无
          <el-input
            oninput ="value=value.replace(/[^0-9.]/g,'')"
            size="mini"
            style="width: 70px"
            v-model.number.trim="queryParams.temperature"
          ></el-input>
          小时内抗原阴性报告
        </el-form-item>
      </el-form>
      <div class="btn">
        <el-button size="small" type="primary" @click="handleSearch"
          >查询</el-button
        >
        <el-button size="small" type="primary" @click="handleReset"
          >重置</el-button
        >
      </div> -->
      <!--  -->
      <div class="mb-10">
        <el-radio-group
          v-model="currnetParamType"
          size="small"
          v-if="currentParam === '学生'"
          @change="handleChangeType"
        >
          <el-radio-button label="学生"></el-radio-button>
          <el-radio-button label="同住人"></el-radio-button>
        </el-radio-group>
        <el-radio-group
          v-model="currnetParamType"
          size="small"
          v-if="currentParam === '教职工'"
          @change="handleChangeType"
        >
          <el-radio-button label="教职工"></el-radio-button>
          <el-radio-button label="同住人"></el-radio-button>
        </el-radio-group>
      </div>
      <el-form
        ref="filterForm"
        :inline="true"
        :model="queryParams"
        label-width="120px"
      >
        <el-form-item label="体温" prop="name" v-if="ruleForm.bodyTemperature">
          ≥<el-input
            disabled
            oninput="value=value.replace(/[^0-9.]/g,'')"
            size="mini"
            style="width: 70px"
            v-model.trim="ruleForm.bodyTemperature"
          ></el-input
          >℃
        </el-form-item>
        <el-form-item
          label="健康码"
          prop="region"
          v-if="ruleForm.healthCodeStatus && ruleForm.healthCodeStatus[0]"
        >
          <el-checkbox-group disabled v-model="ruleForm.healthCodeStatus">
            <el-checkbox label="黄码" name="黄码"></el-checkbox>
            <el-checkbox label="红码" name="红码"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item
          label="核酸"
          prop="region"
          v-if="ruleForm.nucleicAcidStatus && ruleForm.nucleicAcidStatus[0]"
        >
          <el-checkbox-group disabled v-model="ruleForm.nucleicAcidStatus">
            <el-checkbox label="阴性" name="阴性"></el-checkbox>
            <el-checkbox label="阳性" name="阳性"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label=" " v-if="ruleForm.nucleicAcidStatusDays">
          无
          <el-input
            disabled
            oninput="value=value.replace(/[^0-9.]/g,'')"
            size="mini"
            style="width: 70px"
            v-model.number.trim="ruleForm.nucleicAcidStatusDays"
          ></el-input>
          小时内核酸阴性报告
        </el-form-item>
        <el-form-item
          label="抗原"
          prop="region"
          v-if="ruleForm.antigenStatus && ruleForm.antigenStatus[0]"
        >
          <el-checkbox-group disabled v-model="ruleForm.antigenStatus">
            <el-checkbox label="阴性" name="阴性"></el-checkbox>
            <el-checkbox label="阳性" name="阳性"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label=" " v-if="ruleForm.antigenStatusDays">
          无
          <el-input
            disabled
            oninput="value=value.replace(/[^0-9.]/g,'')"
            size="mini"
            style="width: 70px"
            v-model.number.trim="ruleForm.antigenStatusDays"
          ></el-input>
          小时内抗原阴性报告
        </el-form-item>
        <el-form-item
          label="疫苗"
          prop="delivery"
          v-if="ruleForm.vaccineStatus && ruleForm.vaccineStatus[0]"
        >
          <el-checkbox-group disabled v-model="ruleForm.vaccineStatus">
            <el-checkbox label="未接种" name="未接种"></el-checkbox>
            <el-checkbox label="接种一针" name="接种一针"></el-checkbox>
            <el-checkbox label="接种两针" name="接种两针"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item
          v-for="item in surveyList"
          :key="item.prop"
          label=" "
          v-show="ruleForm[item.prop]"
          label-width="40px"
        >
          <span v-if="item.dayNum">近{{ ruleForm[item.dayNum] }}</span
          ><span>{{ item.label }}</span>
          <el-radio-group
            v-model="ruleForm[item.prop]"
            disabled
            style="margin-left: 15px"
          >
            <el-radio label="是">是</el-radio>
            <el-radio label="否">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div class="btn">
        <el-button
          v-if="checkAuthority('/stu/reset') && currnetParamType === '学生'"
          size="small"
          type="primary"
          @click="remake"
          :disabled="!riskId"
          >重置</el-button>
          <el-button
          v-if="checkAuthority('/livewith/reset') && currnetParamType === '同住人'"
          size="small"
          type="primary"
          @click="remake"
          :disabled="!riskId"
          >重置</el-button>
      </div>
    </el-card>

    <el-card class="head-card">
      <div class="grade-class">
        <div class="left">
          <div class="label">高风险人员名单</div>
          <AccNav
            style="margin: 0 40px"
            v-model="currentParam"
            :options="options"
            @click-change="handleChangeParam"
          ></AccNav>
          <el-form
            ref="filterForm"
            :inline="true"
            :model="queryParamsGrade"
            label-width="120px"
          >
            <el-form-item
              label="年级"
              label-width="45px"
              v-if="currentParam === '学生'"
            >
              <el-select
                style="width: 140px"
                filterable
                clearable
                size="small"
                v-model="queryParamsGrade.gradeName"
                placeholder="请选择"
                @change="handleChangeGrade"
              >
                <el-option
                  v-for="item in gradeOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="班级"
              v-if="currentParam === '学生'"
              label-width="45px"
            >
              <el-select
                style="width: 140px"
                filterable
                clearable
                size="small"
                v-model="queryParamsGrade.className"
                placeholder="请选择"
                @change="handleChangeCalss"
              >
                <el-option
                  v-for="item in classOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="姓名" label-width="45px">
              <el-input
                v-model="queryParamsGrade.name"
                size="small"
                style="width: 140px"
                placeholder="请输入"
              />
            </el-form-item>
            <el-form-item class="card-flex" v-if="currentParam !== '学生'">
              <div v-if="checkAuthority('/teacher/eyes')" class="switch-btn cursor iconfont" :class="[isShow ? 'icon-biyan': 'icon-yanjing']" @click="handleClickIsShow"></div>
            </el-form-item>
          </el-form>
        </div>
        <div class="right">
          <el-button size="small" type="primary" @click="handleSearch"
            >查询</el-button
          >
          <el-button v-if="checkAuthority('/student/down')" size="small" type="primary" @click="handleExport"
            >导出</el-button
          >
          <el-button v-if="checkAuthority('/student/notices')" size="small" type="primary" @click="handleNotification"
            >一键通知</el-button
          >
          <el-button v-if="checkAuthority('/student/white')" size="small" type="primary" @click="handleWhiteList"
            >白名单</el-button
          >
        </div>
      </div>
    </el-card>
    <Students
      ref="student"
      v-if="currentParam === '学生'"
      :table="tableList"
      @select-row="handleSelectRow"
    ></Students>
    <TeachingStaff
      ref="teacher"
      v-if="currentParam === '教职工'"
      :table="tableList"
      :isShow="isShow"
      @select-row="handleSelectRow"
    ></TeachingStaff>
    <acc-page
      :total="total"
      :page-size="pageVo.pageSize"
      :current-page="pageVo.pageNum"
      @current-change="handleChangePage"
      @size-change="handleChangeSize"
    ></acc-page>
    <!-- 二期 -->
    <el-drawer
      :visible.sync="visibleDrawer"
      :wrapper-closable="false"
      show-slose
      size="45%"
    >
      <NewPage
        v-if="visibleDrawer"
        :drawerData="{ type: 'edit', id: this.riskId }"
        @click-cancel="handleCancel"
      />
    </el-drawer>
    <el-dialog
      :visible.sync="visibleWhiteList"
      @close="handleClose"
      width="80%"
    >
      <WhileList v-if="visibleWhiteList"></WhileList>
    </el-dialog>
  </div>
</template>

<script>
import AccNav from '@/components/common/AccNav'
import Students from './students'
import TeachingStaff from './teachingStaff'
import NewPage from '../riskRules/new.vue'
import WhileList from './whiteList'
import { getPreventionRiskScreenList, exportPreRiskScreen, savePreventionMsgNotice, findPreventionRiskRulesDetail, getPreventionRiskRulesList } from '@/views/children/antiepidemic/js/preventionRiskScreen'
import { getGradeList, getclassesList } from '@/views/children/antiepidemic/js/base'
import { checkAuthority } from '@/utils/btnPermission' // 页面按钮权限
// import { query } from '@/views/children/antiepidemic/js/dynamicTiming'
export default {
  components: { AccNav, Students, TeachingStaff, NewPage, WhileList },
  data () {
    return {
      queryParams: {
        healthCodeStatus: [],
        vaccineStatus: []
      },
      queryParamsGrade: {
        gradeName: '',
        className: '',
        name: ''
      },
      currentParam: '学生',
      currnetParamType: '学生',
      options: ['学生', '教职工'],
      pageVo: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      gradeOptions: [],
      classOptions: [],
      tableList: [],
      tableSelectRows: [],
      tenantNo: '',
      // updatedTime: '',
      /* 二期新增字段 */
      riskId: '',
      ruleForm: [],
      surveyList: [
        { label: '是否在沪【含上海所在区县（市）】', prop: 'inShFlag' },
        { label: '天是否离开当前所在地', prop: 'leavePossessionFlag', dayNum: 'leavePossessionDays' },
        { label: '天是否与确诊密接或已确诊', prop: 'confirmFlag', dayNum: 'confirmDays' },
        { label: '本人及同住人是否有发热、气喘、咳嗽、呼吸道症状', prop: 'symptomFlag' },
        { label: '当前健康状况是否异常', prop: 'healthFlag' },
        { label: '本人及同住人是否途径中风险地区', prop: 'passModerateAddr' },
        { label: '本人及同住人是否途径高风险地区', prop: 'passHightAddr' }
      ],
      visibleDrawer: false,
      visibleWhiteList: false,
      isShow: false
    }
  },
  created () {
    this.getGrade()
    // 二期去除默认查询 通过条件id查询
    // this.handleSearch()
    this.getRule()
  },
  mounted () {
    let userInfo = localStorage.getItem('Standard__User-Info')
    this.tenantNo = JSON.parse(userInfo).value.tenantNo
    // this.queryCon()
  },
  methods: {
    checkAuthority,
    handleClickIsShow () {
      this.isShow = !this.isShow
    },
    handleClose () {
      this.loadDataList()
    },
    handleRules () {
      this.$router.push({ path: '/riskRules' })
    },
    // 获取更新配置信息
    // queryCon () {
    //   query().then((res) => {
    //     const { code, data } = res
    //     if (code === 0) {
    //       this.updatedTime = data.updatedTime
    //     }
    //   })
    // },
    getGrade () {
      getGradeList({ pageNum: 1, pageSize: 999, status: 1 }).then(res => {
        if (res.code !== 0) return
        this.gradeOptions = res.data.records
      })
    },
    getclasses () {
      getclassesList({ gradeName: this.queryParamsGrade.gradeName, status: 1, pageNum: 1, pageSize: 999 }).then(res => {
        if (res.code !== 0) return
        this.classOptions = res.data.classList.records
      })
    },
    handleSelectRow (rows) {
      this.tableSelectRows = rows
    },
    handleChangeGrade (val) {
      // this.handleSearch()
      this.queryParams.class = null
      this.classOptions = []
      if (val !== '') this.getclasses()
    },
    handleChangeCalss (val) {
      // this.handleSearch()
    },
    // 二期新增
    remake () {
      // 打开规则弹出
      this.visibleDrawer = true
    },
    // 获取规则研判
    async getRule () {
      let userInfo = localStorage.getItem('Standard__User-Info')
      let temp = {
        tenantNo: JSON.parse(userInfo)?.value?.tenantNo,
        status: 1
      }
      temp.roleCode = this.currnetParamType === '学生' ? 2 : this.currnetParamType === '教职工' ? 1 : 3
      const { code, data } = await getPreventionRiskRulesList(temp)
      if (code === 0) {
        let details = data.records.find((item) => item.status === 1)
        this.getRuleDetails(details?.id || '')
      }
    },
    getRuleDetails (id) {
      if (!id) {
        this.riskId = ''
        this.ruleForm = []
        this.handleSearch()
        return
      }
      findPreventionRiskRulesDetail({ id }).then(res => {
        const { code, data } = res
        if (code === 0) {
          this.riskId = data.id
          this.ruleForm = res.data
          this.handleSearch()
        }
      })
    },
    handleCancel (value) {
      this.visibleDrawer = false
      if (value) {
        this.getRule()
        this.handleSearch()
      }
    },
    /* 二期信息方法结束 */
    handleReset () {
      this.queryParams = {
        healthCodeStatus: [],
        vaccineStatus: []
      }
      this.loadDataList()
    },
    handleSearch () {
      this.pageVo.pageNum = 1
      this.loadDataList()
    },
    handleChangeParam () {
      this.pageVo.pageNum = 1
      this.tableSelectRows = []
      this.queryParamsGrade.gradeName = ''
      this.queryParamsGrade.className = ''
      this.queryParamsGrade.name = ''
      this.currnetParamType = this.currentParam === '学生' ? '学生' : '教职工'
      // this.loadDataList()
      this.getRule()
    },
    handleChangeType () {
      this.getRule()
    },
    loadDataList () {
      /* 一期查询逻辑 */
      let type = {
        学生: 2,
        教职工: 1
      }
      // let temp = {}
      // this.tableList = []
      // Object.assign(temp, this.queryParams, this.pageVo, this.queryParamsGrade, { userType: type[this.currentParam] })
      // if (temp.healthCodeStatus.length === 0) delete temp.healthCodeStatus
      // if (temp.vaccineStatus.length === 0) delete temp.vaccineStatus
      // let userInfo = localStorage.getItem('Standard__User-Info')
      // temp.tenantNo = JSON.parse(userInfo).value.tenantNo
      // getPreventionRiskScreenList(temp).then(res => {
      //   this.tableList = res.data.records || []
      //   this.total = res.data.total
      //   this.$nextTick(() => {
      //     this.tableList.forEach(item => {
      //       if (this.tableSelectRows.findIndex(i => i.id === item.id) > -1) {
      //         if (this.currentParam === '学生') this.$refs.student.$refs.accTable.$refs._accTable.toggleRowSelection(item, true)
      //         if (this.currentParam === '教职工') this.$refs.teacher.$refs.accTable.$refs._accTable.toggleRowSelection(item, true)
      //       } else {
      //         if (this.currentParam === '学生') this.$refs.student.$refs.accTable.$refs._accTable.toggleRowSelection(item, false)
      //         if (this.currentParam === '教职工') this.$refs.teacher.$refs.accTable.$refs._accTable.toggleRowSelection(item, false)
      //       }
      //     })
      //   })
      // })
      /* 二期查询逻辑 */
      getPreventionRiskScreenList({
        userType: type[this.currentParam],
        riskRulesId: this.riskId,
        ...this.queryParamsGrade,
        ...this.pageVo
      }).then(res => {
        this.tableList = res.data.records || []
        this.total = res.data.total
        this.$nextTick(() => {
          this.tableList.forEach(item => {
            if (this.tableSelectRows.findIndex(i => i.id === item.id) > -1) {
              if (this.currentParam === '学生') this.$refs.student.$refs.accTable.$refs._accTable.toggleRowSelection(item, true)
              if (this.currentParam === '教职工') this.$refs.teacher.$refs.accTable.$refs._accTable.toggleRowSelection(item, true)
            } else {
              if (this.currentParam === '学生') this.$refs.student.$refs.accTable.$refs._accTable.toggleRowSelection(item, false)
              if (this.currentParam === '教职工') this.$refs.teacher.$refs.accTable.$refs._accTable.toggleRowSelection(item, false)
            }
          })
        })
      })
    },
    handleChangePage (page) {
      this.pageVo.pageNum = page
      this.loadDataList()
    },
    handleChangeSize (size) {
      this.pageVo.pageSize = size
      this.handleSearch()
    },
    handleExport () {
      let type = {
        学生: 2,
        教职工: 1
      }
      exportPreRiskScreen({ exportType: type[this.currentParam], ...this.queryParamsGrade, idList: this.tableSelectRows.map(i => i.id) }).then(res => {
        this.$message.success('导出成功')
      })
    },
    handleNotification () {
      if (this.tableSelectRows.length === 0) return this.$message.warning('未选择通知人员')
      let list = []
      list = this.tableSelectRows.filter(item => item.status === 0).map(item => item.id)
      let status1 = this.tableSelectRows.filter(item => item.status !== 0).map(item => item.name)
      if (list.length === 0) return this.$message.warning('当前选择人员已通知，不再继续通知')
      savePreventionMsgNotice({ idList: list }).then(() => {
        let text = status1.length > 0 ? `${status1.toString()}当前状态已通知，不再继续通知。其他选择的人员已通知` : '人员通知成功'
        this.$message.success(text)
        this.tableSelectRows = []
        this.handleSearch()
      })
    },
    handleWhiteList () {
      this.visibleWhiteList = true
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  display: flex;
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 10px;
  justify-content: space-between;
}
.btn {
  text-align: center;
}
.head-card {
  .el-form-item {
    margin-bottom: 0;
  }
}
.grade-class {
  display: flex;
  justify-content: space-between;
  .left {
    display: flex;
    align-items: center;
    .label {
      font-size: 18px;
      font-weight: 550;
    }
  }
  .right {
    width: 300px;
  }
}
</style>
